from django.shortcuts import render

from .models import Fiction

import requests
from bs4 import BeautifulSoup


"""
需要安装：
    pip install requests
    pip install beautifulsoup4==4.7.0
    pip install lxml
"""


url = 'https://www.e1w.net/'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'
}


def index(request):
    if request.method == 'GET':
        # 当访问方式为get时，开始请求数据
        result = requests.get(url=url, headers=headers)
        # 继承网页当中的编码类型
        result.encoding = result.apparent_encoding
        # 实例化bs4
        # text: 这是获取文本   json: 获取json数据  content: 获取二进制内容
        html_obj = BeautifulSoup(result.text, features='lxml')
        # 开始解析网页内容
        li_list = html_obj.find('ul', {'class': 'update'}).find_all('li')[:-1]
        # print(li_list)
        fiction_list = []
        for li in li_list:
            fiction_list.append(Fiction(
                title=li.find('a', {'class': 'poptext'}).text,
                href=li.find('a', {'class': 'poptext'})['href'],
                cate=li.find('p', {'class': 'ul1'}).find('a').text,
                author=li.find_all('p')[2].text
            ))

        Fiction.objects.bulk_create(fiction_list)
        return render(request, 'index.html')




